#include<bits/stdc++.h>
using namespace std;
inline int read()
{
    int x=0,sign=1;
    char c;
    c=getchar();
    while(c>'9'||c<'0')
    {
        if(c=='-') sign=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        x=x*10+(c-'0');
        c=getchar();
    }
    return x*sign;
}
int n;
int a[200005];
bool b[200005];
int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int T;
    int a1,a2,a3,a4,s1,t;
    T=read();
    while(T--)
    {
        t=0;
        int ans=0;
        n=read();
        memset(b,0,sizeof(b));
        for(int i=1;i<=n;i++) a[i]=read();
        for(int i=1;i<=n;i++)
        {
            if(b[i]==1) continue;
            b[i]==1;
            if(a1<i)
            {
                t--;
                a1=-1;
            }
            if(a3<i)
            {
                t--;
                a3=-1;
            }
            for(int j=i+1;j<=n;j++)
            {
                if(a[j]==a[i])
                {
                    ans+=a[i];
                    b[j]=1;
                    if(t==2)
                    {
                        if(a[j]>a2&&a[j]>a4)
                        {
                            if(a2>a4)
                            {
                                ans-=a4;
                                a3=j;
                            }
                            else
                            {
                                ans-=a2;
                                a1=j;
                            }
                        }
                        if(a2>a[j]&&a2>a4)
                        {
                            if(a[j]>a4)
                            {
                                ans-=a4;
                                a3=j;
                            }
                            else
                            {
                                ans-=a[j];
                            }
                        }
                        if(a4>a[j]&&a4>a2)
                        {
                            if(a[j]>a2)
                            {
                                ans-=a2;
                                a1=j;
                            }
                            else
                            {
                                ans-=a[j];
                            }
                        }
                    }
                    if(t<2)
                    {
                        t++;
                        if(a1==-1)
                        {
                            a2=a[j];
                            a1=j;
                        }
                        else if(a3==-1)
                        {
                            a4=a[j];
                            a1=j;
                        }
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
